package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyMenu;
import com.jy.adscm.bean.system.JyRole;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.system.mapper.mysql.JyMenuMapper;
import com.jy.adscm.zltx.bean.JyZltxListTree;
import com.jy.adscm.zltx.bean.QsFile;
import com.jy.adscm.zltx.mapper.mysql.JyZltxListTreeMapper;
import com.jy.adscm.zltx.mapper.mysql.QsFileMapper;
import com.jy.adscm.zltx.service.JyZltxListTreeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * <p>
 * 质量体系树形结构表 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2018-11-02
 */
@Service
public class JyZltxListTreeServiceImpl extends ServiceImpl<JyZltxListTreeMapper, JyZltxListTree> implements JyZltxListTreeService {

    @Autowired
    JyZltxListTreeMapper jyZltxListTreeMapper;
    @Autowired
    QsFileMapper qsFileMapper;
    @Autowired
    JyMenuMapper jyMenuMapper;
    Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public JyZltxListTree selectListTreeByMenuId(Integer id) throws AdscmSystemException {

        return jyZltxListTreeMapper.selectById(id);
    }

    @Override
    public void tttt() {
        List<QsFile> qsFiles = qsFileMapper.selectList(null);

        for (QsFile qsFile : qsFiles) {
            String fileGs = qsFile.getFileGs();

            Wrapper<JyZltxListTree> wrapper = new EntityWrapper<>();
            wrapper.like("file_gs","%"+fileGs);

            List<JyZltxListTree> trees = jyZltxListTreeMapper.selectList(wrapper);
            if (trees != null && trees.size()>0){
                JyZltxListTree zltxListTree = trees.get(0);
                zltxListTree.setLinkFileId(qsFile.getFileId());
                jyZltxListTreeMapper.updateById(zltxListTree);

            }

        }
        
        
    }
    @Cacheable(cacheNames = {"zltxListTree"},key = "#user.id")
    @Override
    public List<JyZltxListTree> selectAll(Long id, JyUser user) throws AdscmSystemException {

        logger.info("获取质量体系树");

        //查看当前用户的角色，能操作那些质量体系的树；
        Set<Integer> roleIds = new HashSet<>();
        if(id == 34){//程序文件的，才用角色控制
            List<JyRole> roles = user.getRoles();
            if (roles != null && roles.size()>0){
                for (JyRole role : roles) {
                    roleIds.add(role.getId());
                }
            }
        }



        Wrapper<JyZltxListTree> wrapper = new EntityWrapper<>();
        wrapper.eq("id",id);

        List<JyZltxListTree> topList = jyZltxListTreeMapper.selectList(wrapper);

        getAllChildren(topList,new ArrayList<>(roleIds));

        return topList;
    }



    private void getAllChildren(List<JyZltxListTree> parentList,List<Integer> roleIds){

        if (parentList != null && parentList.size()>0){
            for (JyZltxListTree zltxListTree : parentList) {

//                Wrapper<JyZltxListTree> wrapper = new EntityWrapper<>();
//                wrapper.eq("parent_id",zltxListTree.getId());
//                wrapper.in("id",roleIds);
                if (zltxListTree.getParentId() == 34){
                    roleIds = null;
                }
                List<JyZltxListTree> children = jyZltxListTreeMapper.getZltxListTreeList(zltxListTree.getId(),roleIds);

                if (children != null && children.size()>0){
                    zltxListTree.setChildren(children);
                    getAllChildren(children,roleIds);
                }

            }
        }


    }


    @Override
    public List<JyMenu> getMenuAllSrc(int menuId, JyUser user) {



        JyZltxListTree zltxListTree = jyZltxListTreeMapper.selectById(menuId);

        JyZltxListTree parentTree = jyZltxListTreeMapper.selectById(zltxListTree.getParentId());

        List<Long> ids = new ArrayList<>();
        ids.add(zltxListTree.getId());
        if (parentTree != null){
            ids.add(parentTree.getId());
            ids.add(parentTree.getParentId());
        }

        List<Integer> zltxRoles = jyZltxListTreeMapper.getZltxRole(ids);
        Map<Integer,Integer> map = new HashMap<>();
        if (zltxRoles != null && zltxRoles.size()>0){
            for (Integer zltxRole : zltxRoles) {
                map.put(zltxRole,zltxRole);
            }
        }

        Wrapper<JyMenu> wrapper_menu = new EntityWrapper<>();
        wrapper_menu.eq("parentId",-1000);
        List<JyMenu> menus = jyMenuMapper.selectList(wrapper_menu);



        List<JyRole> roles = user.getRoles();

        if (roles != null && roles.size()>0){
            for (JyRole role : roles) {
                Integer roleId = role.getId();
                if (map.get(roleId) != null){
                    String role_code = role.getRole_code();
                    role_code = role_code.substring(role_code.lastIndexOf("_") + 1);
                    for(JyMenu menu : menus){
                        if (role_code.equals(menu.getComponent())){
                            menu.setMenuShow(true);
                            menu.setName(role.getRole_name());
                            break;
                        }
                    }
                }
            }
        }



        return menus;
    }
}
